import { login, before } from '../services/login';

export default {
  namespace: 'userLogin',

  state: {
    data: null, // 用户登录后的所有数据
    beforeData: null, // 学校用户登录前的所有数据
  },

  effects: {
    *submit({ payload }, { call, put }) {
      const response = yield call(login, payload);
      yield put({ type: 'callbackSubmit', response });
    },
    *before(_, { call, put }) {
      const response = yield call(before);
      yield put({ type: 'callbackBefore', response });
    },
  },

  reducers: {
    callbackSubmit(st, { response }) {
      const state = st;
      state.data = response;
      return { ...state };
    },
    callbackBefore(st, { response }) {
      const state = st;
      if (response.success) state.beforeData = response.data;
      return { ...state };
    },
  },
};
